package com.wanxi.login.service.impl;

import com.wanxi.login.api.LoginFeign;
import com.wanxi.login.domain.dto.LoginDefaultDto;
import com.wanxi.login.domain.dto.LoginDto;
import com.wanxi.login.domain.dto.LoginParamsDto;
import com.wanxi.login.domain.dto.PasswordDto;
import com.wanxi.login.domain.entity.TokenVo;
import com.wanxi.login.domain.result.LoginResult;
import com.wanxi.login.entity.Login;
import com.wanxi.login.mapper.LoginMapper;
import com.wanxi.login.util.JwtTokenUtil;
import com.wanxi.login.util.LoginParamsToLogin;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author:Iny
 * @Data：2021/12/31 11:11
 */
@RestController
@RequestMapping("/back")
public class LoginServiceImpl implements LoginFeign {

    @Resource
    private LoginMapper loginMapper;

    @Resource
    private PasswordEncoder passwordEncoder;

    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @Override
    @ApiOperation(value = "注册接口")
    @PostMapping("addLogin")
    public LoginResult<Boolean> addLogin(LoginDto loginDto) {
        //判断是否已被注册
        Login byUserName = loginMapper.findByUserName(loginDto.getUsername());
        if (byUserName!=null){
            return LoginResult.addLoginFailed();
        }
        //密码加密
        String encode = passwordEncoder.encode(loginDto.getPassword());
        loginDto.setPassword(encode);
        int i = loginMapper.addLogin(loginDto);
        if (i>0){
            return LoginResult.success(i>0);
        }
        return LoginResult.failed();
    }


    /**
     *
     * @param loginParamsDto 登陆信息
     * @return
     */

    @Override
    @ApiOperation(value = "登陆接口")
    @PostMapping("/login")
    public LoginResult<TokenVo> login(LoginParamsDto loginParamsDto) {
        Login login = LoginParamsToLogin.loginParamsToLogin(loginParamsDto);
        String username = login.getUsername();
        Assert.notNull(username,"账号必须不能为空");
        String password = login.getPassword();
        Assert.notNull(password,"密码必须不能为空");
        // 根据用户查询用户实例
        Login userByName = loginMapper.findByUserName(username);
        if (userByName.getIsDelete()==1){
            return LoginResult.failed("账号已被注销！");
        }
        // 验证角色
        if(userByName.getRole()!=loginParamsDto.getRole()){
            return LoginResult.failed();
        }
        // 验证密码
        boolean matches = passwordEncoder.matches(password, userByName.getPassword());
        if(matches){

            // 使用jwtTokenUtil 生成token
            return LoginResult.success( new TokenVo(userByName.getId(),userByName.getRoleId(),jwtTokenUtil.generateToken(userByName)));
        }
        return LoginResult.failed();
    }

    /**
     *
     * @param passwordDto  登陆id和新密码
     * @return
     */
    @Override
    public LoginResult<Boolean> updatePasswordByUserName(PasswordDto passwordDto) {
        String encode = passwordEncoder.encode(passwordDto.getNewPassword());
        passwordDto.setNewPassword(encode);
        int update = loginMapper.updatePassword(passwordDto);
        if (update>0){
            return LoginResult.success(update>0);
        }
        return LoginResult.failed(update>0);
    }

    @Override
    public LoginResult<Boolean> updateDefaultId(LoginDefaultDto loginDefaultDto) {
        int i = loginMapper.updateDefaultId(loginDefaultDto);
        if (i>0){
            return LoginResult.success(i>0);
        }
        return LoginResult.failed(i>0);
    }


    @GetMapping("findRoleIdById")
    @Override
    public LoginResult<Long> findRoleIdById(Long id) {
        Long i = loginMapper.findRoleIdById(id);
        if (i!=null){
            return LoginResult.success(i);
        }
        return LoginResult.failed();
    }


}
